package com.itheima.controller;

import cn.itheima.health.entity.Result;
import cn.itheima.health.resources.MessageConstant;
import cn.itheima.health.resources.RedisMessageConstant;
import cn.itheima.health.resources.ValidateCodeUtils;
import cn.itheima.health.service.MemberService;
import com.itheima.pojo.Member;
import com.sun.tools.corba.se.idl.constExpr.Times;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private MemberService memberService;



    @RequestMapping("/sendCodeNumberToPhone.do")
    public Result sendCodeNumberToPhone(@RequestParam("telephone") String  telephone){
        try {
            //  用户手机发送随机验证码
            String  code  = ValidateCodeUtils.generateValidateCode(4);
            //  保存redis  90s
            Jedis jedis = jedisPool.getResource();
            jedis.setex(telephone+"_"+ RedisMessageConstant.SENDTYPE_LOGIN,90,code);
            jedis.close();
            //  发送短信
//            SMSUtils.sendCode(telephone,code);
            System.out.println("---login telephone code ==="+code);
            return  new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

    }




    @RequestMapping("/validateCodeNumberByTelephone.do")
    public Result validateCodeNumberByTelephone(@RequestParam("telephone") String  telephone,@RequestParam("code") String code){
        try {

            Jedis jedis = jedisPool.getResource();

            String redisCode = jedis.get(telephone + "_" + RedisMessageConstant.SENDTYPE_LOGIN);
            if(redisCode==null||redisCode.length()==0){
                return  new Result(false, "验证码已经过期");
            }
            if(!code.equals(redisCode)){
                //  4位  但是 用户输入验证码和redis 不一致
                return  new Result(false, "验证码错误");
            }

            jedis.close();
            //  发送短信
            return  new Result(true,"验证码正确");
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

    }


    @RequestMapping("/login.do")
    public Result login(@RequestParam("telephone") String  telephone,@RequestParam("code") String code){
        try {
            //  保存redis  90s
            Jedis jedis = jedisPool.getResource();
            String redisCode =  jedis.get(telephone+"_"+ RedisMessageConstant.SENDTYPE_LOGIN);
           //   判断新、老用户
            Member  member= memberService.findMemberByTelephone(telephone);
            if(member==null){
                // insert
                member = new Member();
                Date  rtime = new Date(System.currentTimeMillis());
                member.setRegTime(rtime);
                member.setPhoneNumber(telephone);
                memberService.add(member);
            }else{
                 //  老用户 比对验证码
                if(redisCode==null||redisCode.length()==0){
                    return  new Result(false,"验证码过期");
                }
                if(!redisCode.equals(code)){
                    return  new Result(false,"验证码错误");
                }
            }
            jedis.close();
            //  发送短信
            return  new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

    }
}
